package com.zz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zz.entity.Manager;
import com.zz.entity.Supplier;
import com.zz.service.SupplierService;
import com.zz.util.KeyUtil;

@RestController
@RequestMapping("supplier")
public class SupplierController {
	@Resource
	SupplierService supplierService;
	@RequestMapping("findAll")
	public Page<Supplier>findAll(HttpServletRequest request){
		String startpage=request.getParameter("start");
		System.out.println("startpage=="+startpage);
		return supplierService.findAll(startpage, "5");
	}
	@RequestMapping("findByid")
	public Optional<Supplier> findByid(HttpServletRequest request) {
		String wid=request.getParameter("wid");
		System.out.println("wid==="+wid);
		return supplierService.findById(wid);
	}
	@RequestMapping("update")
	public Supplier update(HttpServletRequest request) {
		String id=request.getParameter("upwid");
		String name=request.getParameter("name");
		String tel=request.getParameter("tel");
		String address=request.getParameter("address");
		String notes=request.getParameter("notes");
		String administrator=request.getParameter("administrator");
		Supplier supplier=new Supplier();
		supplier.setId(id);
		supplier.setName(name);
		supplier.setTel(tel);
		supplier.setAddress(address);
		supplier.setNotes(notes);
		supplier.setAdministrator(administrator);
		return supplierService.updatesupplier(supplier);
	}	
	@RequestMapping("delete")
	public Map deletesupplierById(HttpServletRequest request) {
		String id=request.getParameter("deletewid");
		boolean rs=false;
		Optional<Supplier> h=supplierService.findById(id);
		if(h!=null) {
			rs=true;
			supplierService.deleteById(id);			
	}else {
		rs=false;
	}
		Map map=new HashMap();
		map.put("deleteResult", rs);
		return map;
}
	@RequestMapping("findByName")
	public List<Supplier> findByName(HttpServletRequest request){
		String name=request.getParameter("selectname");
		return supplierService.findByName(name);
	}
	@RequestMapping("insert")
	public Supplier insertsupplier(HttpServletRequest request) {
		String name=request.getParameter("insertname");
		String tel=request.getParameter("inserttel");
		String address=request.getParameter("insertadress");
		String notes=request.getParameter("insertnotes");
		String administrator=request.getParameter("insertadministrator");
		Supplier supplier=new Supplier();
		supplier.setId("GYS"+KeyUtil.genUniqueKey());
		supplier.setName(name);
		supplier.setTel(tel);
		supplier.setAddress(address);
		supplier.setNotes(notes);
		supplier.setAdministrator(administrator);
		return supplierService.savesupplier(supplier);
	}
	@RequestMapping("helloname")
	public Map helloname(HttpServletRequest request) {
		HttpSession session=request.getSession();
		Manager m=(Manager) session.getAttribute("loginMana");
		Map map=new HashMap();
		map.put("loginMana", m);
		return map;		
	}
}
